package com.ywh.floating

import android.widget.FrameLayout
import androidx.annotation.LayoutRes
import kotlin.reflect.KClass

/**
 * Create by yangwenhao on 2022/8/5
 */
class FloatingConfig {
    internal var logEnable: Boolean = false
    internal var mLayoutId: Int = 0

    //拖拽
    internal var mDragEnable: Boolean = true

    //吸边
    internal var mSuctionEdge: Boolean = true
    internal var initialX = 0
    internal var initialY = 0
    internal var mFilterClassList: ArrayList<KClass<*>> = ArrayList()
    internal var mOnlyShowClassList: ArrayList<KClass<*>> = ArrayList()
    internal var rootViewCallback: ((FrameLayout)->Unit)? = null

    internal fun canAttachClass(kClass: KClass<*>): Boolean {
        if (mOnlyShowClassList.isNotEmpty()) return mOnlyShowClassList.contains(kClass)
        if (mFilterClassList.isNotEmpty()) return !mFilterClassList.contains(kClass)
        return true
    }

    class Builder {
        private var logEnable: Boolean = false
        private var layoutId: Int = 0
        private var dragEnable: Boolean = true
        private var mSuctionEdge: Boolean = true
        private var initialX = 0
        private var initialY = 0
        private var mFilterClassList: ArrayList<KClass<*>> = ArrayList()
        private var mOnlyShowClassList: ArrayList<KClass<*>> = ArrayList()
        private var rootViewCallback: ((FrameLayout)->Unit)? = null

        fun setLayoutId(@LayoutRes layoutId: Int, rootView: ((FrameLayout)->Unit)? = null): Builder {
            this.layoutId = layoutId
            this.rootViewCallback = rootView
            return this
        }

        fun setDragEnable(dragEnable: Boolean): Builder {
            this.dragEnable = dragEnable
            return this
        }

        fun setSuctionEdge(suctionEdge: Boolean): Builder {
            this.mSuctionEdge = suctionEdge
            return this
        }

        fun setInitialLocation(x: Int, y: Int): Builder {
            this.initialX = x
            this.initialY = y
            return this
        }

        fun addFilterActivity(vararg kClasses: KClass<*>) {
            kClasses.forEach {
                if (!mFilterClassList.contains(it)) {
                    mFilterClassList.add(it)
                }
            }
        }

        fun addOnlyShowActivity(vararg kClasses: KClass<*>) {
            kClasses.forEach {
                if (!mOnlyShowClassList.contains(it)) {
                    mOnlyShowClassList.add(it)
                }
            }
        }

        fun setLogEnable(logEnable: Boolean): Builder {
            this.logEnable = logEnable
            return this
        }

        internal fun build(): FloatingConfig {
            return FloatingConfig().apply {
                mLayoutId = this@Builder.layoutId
                logEnable = this@Builder.logEnable
                mDragEnable = this@Builder.dragEnable
                mSuctionEdge = this@Builder.mSuctionEdge
                initialX = this@Builder.initialX
                initialY = this@Builder.initialY
                mFilterClassList = this@Builder.mFilterClassList
                mOnlyShowClassList = this@Builder.mOnlyShowClassList
                rootViewCallback = this@Builder.rootViewCallback
            }
        }

    }

}